package com.cloud.weixin.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.weixin.project.mapper.WxMenuMapper;
import com.cloud.weixin.project.model.dto.Menu;
import com.cloud.weixin.project.model.dto.MenuButton;
import com.cloud.weixin.project.model.entity.WxMenu;
import com.cloud.weixin.project.service.IWxMenuService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 微信自定义菜单 服务实现类
 * </p>
 *
 * @author Sam
 * @since 2021-04-26
 */
@Service
public class WxMenuServiceImpl extends ServiceImpl<WxMenuMapper, WxMenu> implements IWxMenuService {

    @Autowired
    private WxMpService wxService;

    @Override
    public Menu getWxMenuButton() {
        Menu menu = new Menu();
        //查出一级菜单
        List<WxMenu> listWxMenu = baseMapper.selectList(Wrappers.<WxMenu>lambdaQuery()
                .eq(WxMenu::getParentId, "0").orderByAsc(WxMenu::getSort));
        List<MenuButton> listMenuButton = new ArrayList<>();
        MenuButton menuButton;
        List<MenuButton> subButtons;
        MenuButton subButton;
        if (listWxMenu != null && listWxMenu.size() > 0) {
            for (WxMenu wxMenu : listWxMenu) {
                menuButton = new MenuButton();
                menuButton.setName(wxMenu.getName());
                String type = wxMenu.getType();
                if (StringUtils.isNotBlank(type)) {//无二级菜单
                    menuButton.setType(type);
                    setButtonValue(menuButton, wxMenu);
                } else {//有二级菜单
                    //查出二级菜单
                    List<WxMenu> subMenuList = baseMapper.selectList(Wrappers.<WxMenu>lambdaQuery()
                            .eq(WxMenu::getParentId, wxMenu.getId()).orderByAsc(WxMenu::getSort));
                    subButtons = new ArrayList<>();
                    for (WxMenu subMenu : subMenuList) {
                        subButton = new MenuButton();
                        subButton.setName(subMenu.getName());
                        subButton.setType(subMenu.getType());
                        setButtonValue(subButton, subMenu);
                        subButtons.add(subButton);
                    }
                    menuButton.setSub_button(subButtons);
                }
                listMenuButton.add(menuButton);
            }
        }
        menu.setButton(listMenuButton);
        return menu;
    }

    void setButtonValue(MenuButton menuButton, WxMenu wxMenu) {
        menuButton.setKey(wxMenu.getId());
        menuButton.setUrl(wxMenu.getUrl());
        menuButton.setContent(JSON.parseObject(wxMenu.getContent()));
        menuButton.setRepContent(wxMenu.getRepContent());
        menuButton.setMedia_id(wxMenu.getRepMediaId());
        menuButton.setRepType(wxMenu.getRepType());
        menuButton.setRepName(wxMenu.getRepName());
        menuButton.setAppid(wxMenu.getMaAppId());
        menuButton.setPagepath(wxMenu.getMaPagePath());
        menuButton.setUrl(wxMenu.getUrl());
        menuButton.setRepUrl(wxMenu.getRepUrl());
        menuButton.setRepHqUrl(wxMenu.getRepHqUrl());
        menuButton.setRepDesc(wxMenu.getRepDesc());
        menuButton.setRepThumbMediaId(wxMenu.getRepThumbMediaId());
        menuButton.setRepThumbUrl(wxMenu.getRepThumbUrl());
    }

    /**
     * 保存并发布菜单
     *
     * @param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndRelease(Menu menu) throws WxErrorException {
        List<MenuButton> buttons = menu.getButton();
        //先删除
        baseMapper.delete(Wrappers.emptyWrapper());
        WxMenu wxMenu = null;
        WxMenu wxMenu1 = null;
        int sort1 = 1;
        int sort2 = 1;
        //入库
        for (MenuButton menuButton : buttons) {
            wxMenu = new WxMenu();
            setWxMenuValue(wxMenu, menuButton);
            wxMenu.setSort(sort1);
            wxMenu.setParentId("0");
            baseMapper.insert(wxMenu);
            menuButton.setKey(wxMenu.getId());
            sort1++;
            for (MenuButton menuButton1 : menuButton.getSub_button()) {
                wxMenu1 = new WxMenu();
                setWxMenuValue(wxMenu1, menuButton1);
                wxMenu1.setSort(sort2);
                wxMenu1.setParentId(wxMenu.getId());
                baseMapper.insert(wxMenu1);
                menuButton1.setKey(wxMenu1.getId());
                sort2++;
            }
        }
        //创建菜单
        wxService.getMenuService().menuCreate(menu.toString());
    }

    void setWxMenuValue(WxMenu wxMenu, MenuButton menuButton) {
        wxMenu.setId(menuButton.getKey());
        wxMenu.setType(menuButton.getType());
        wxMenu.setName(menuButton.getName());
        wxMenu.setUrl(menuButton.getUrl());
        wxMenu.setRepMediaId(menuButton.getMedia_id());
        wxMenu.setRepType(menuButton.getRepType());
        wxMenu.setRepName(menuButton.getRepName());
        wxMenu.setMaAppId(menuButton.getAppid());
        wxMenu.setMaPagePath(menuButton.getPagepath());
        wxMenu.setRepContent(menuButton.getRepContent());
        wxMenu.setContent(menuButton.getContent().toJSONString());
        wxMenu.setRepUrl(menuButton.getRepUrl());
        wxMenu.setRepHqUrl(menuButton.getRepHqUrl());
        wxMenu.setRepDesc(menuButton.getRepDesc());
        wxMenu.setRepThumbMediaId(menuButton.getRepThumbMediaId());
        wxMenu.setRepThumbUrl(menuButton.getRepThumbUrl());
        menuButton.setRepUrl(null);
        menuButton.setRepDesc(null);
        menuButton.setRepHqUrl(null);
        menuButton.setContent(null);
        menuButton.setRepContent(null);
        menuButton.setRepType(null);
        menuButton.setRepName(null);
        menuButton.setRepThumbMediaId(null);
        menuButton.setRepThumbUrl(null);
    }
}
